package com.topboya.linker.model;


import com.topboya.linker.model.levelitem.FollowUserGroup;
import com.topboya.linker.model.response.CompanyUserListReponse;
import com.topboya.linker.model.response.FollowUserByOfficeResponse;
import com.topboya.linker.model.response.FollowUserLateResponse;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class FollowUserDataManager {

    private static final String TAG = "FollowUserDataManager";

    private List<FollowUserGroup> selectfollowuser;
    public List<FollowUserGroup> getlateFollowUserlist(List<FollowUserLateResponse.LateUser> lateUserlist,List<FollowUserGroup> selectfollowuser){
         this.selectfollowuser = selectfollowuser;
           List<FollowUserGroup> followUserGroups = new ArrayList<>();
           for (int i =0;i < lateUserlist.size();i++){

               FollowUserGroup followUserGroup = new FollowUserGroup();
               followUserGroup.setName(lateUserlist.get(i).getName());
               followUserGroup.setId(lateUserlist.get(i).getUserId());
               followUserGroups.add(followUserGroup);
               if (null !=selectfollowuser){
               for (int j =0;j <selectfollowuser.size();j++){
                   if (selectfollowuser.get(j).getName().equals(lateUserlist.get(i).getName())){

                       followUserGroup.setCheked(true);
                   }
               }
               }
           }
          return  followUserGroups;
      }

      public List<FollowUserGroup> getSameFollowUserlist(List<FollowUserByOfficeResponse.SameDepartList> sameDepartLists,List<FollowUserGroup> selectfollowuser){
          this.selectfollowuser = selectfollowuser;

          List<FollowUserGroup> followUserGroups = new ArrayList<>();

           for (int i=0;i < sameDepartLists.size();i++){

               FollowUserGroup followUserGroup = new FollowUserGroup();
               followUserGroup.setName(sameDepartLists.get(i).getName());
               followUserGroup.setId(sameDepartLists.get(i).getId());
               followUserGroups.add(followUserGroup);

               if (null !=selectfollowuser){
                   for (int j =0;j <selectfollowuser.size();j++){
                       if (selectfollowuser.get(j).getName().equals(sameDepartLists.get(i).getName())){

                           followUserGroup.setCheked(true);
                       }
                   }
               }
           }

          return  followUserGroups;
      }

      public List<FollowUserGroup> getCompanyUserList(List<CompanyUserListReponse.Data> comuserlists,List<FollowUserGroup> selectfollowuser){
          this.selectfollowuser = selectfollowuser;

          List<FollowUserGroup> followUserGroups = new ArrayList<>();

          for (int i = 0; i < comuserlists.size(); i++) {

              CompanyUserListReponse.Data data = comuserlists.get(i);
              List<CompanyUserListReponse.SubDeptList> subDeptListsroot = data.getSubDeptList();
              List<CompanyUserListReponse.DeptUserList> deptUserListsroot = data.getDeptUserList();

              if (null !=subDeptListsroot){

                  FollowUserGroup followUserGroup = new FollowUserGroup();
                  followUserGroup.setName(data.getName());
                  followUserGroup.setSub(true);
                  followUserGroup.setSelectNum("0");
                  followUserGroup.setSubListss(data.getSubDeptList());
                  followUserGroup.setDeptListss(data.getDeptUserList());
                  followUserGroup.setDeptGroupBosPs(null);
                  followUserGroup.setDeptGroupBoP(null);
                  List<FollowUserGroup> children = generateChild(followUserGroups,followUserGroup,false);
                  followUserGroup.setSubGroupBosCd(children);
                  followUserGroups.add(followUserGroup);

              }
              if (null !=deptUserListsroot){

                  FollowUserGroup followUserGroup = new FollowUserGroup();
                  followUserGroup.setName(data.getName());
                  followUserGroup.setSub(false);
                  followUserGroup.setDeptGroupBosPs(null);
                  followUserGroup.setDeptGroupBoP(null);
                  followUserGroup.setId(data.getId());
                  followUserGroups.add(followUserGroup);
                  if (null !=selectfollowuser){
                      for (int j =0;j <selectfollowuser.size();j++){

                          if (selectfollowuser.get(j).getName().equals(data.getName())){

                              followUserGroup.setCheked(true);
                          }
                      }
                  }
              }
          }
          return  followUserGroups;
      }

    private List<FollowUserGroup>  generateChild(List<FollowUserGroup> followUserGroupList, FollowUserGroup group, boolean ischecked ) {
        List<FollowUserGroup> followUserGroupBos = new ArrayList<>();
        List<FollowUserGroup>  dchild = getNextData(group,ischecked);
        if (null != dchild){

            LogUtils.v(TAG + "dchild:"+dchild.toString());
            followUserGroupBos.addAll(dchild);

            for (int i=0;i < dchild.size();i++){

                FollowUserGroup item = dchild.get(i);

                item.setDeptGroupBosPs(followUserGroupList);
                item.setDeptGroupBoP(group);
                List<FollowUserGroup> child = generateChild(dchild,item,ischecked);
                item.setSubGroupBosCd(child);

            }
        }
        LogUtils.v(TAG + "followUserGroupBos:"+followUserGroupBos.toString());

        return followUserGroupBos;
    }

    private List<FollowUserGroup> getNextData(FollowUserGroup followUserGroup,boolean checked) {

        List<CompanyUserListReponse.SubDeptList> cDeptList = followUserGroup.getSubListss();
        List<CompanyUserListReponse.DeptUserList> cUserList = followUserGroup.getDeptListss();

        List<FollowUserGroup> result = new ArrayList<>();

        if (null != cDeptList) {
            for (int i = 0; i < cDeptList.size(); i++) {
                FollowUserGroup group = new FollowUserGroup();
                CompanyUserListReponse.SubDeptList list = cDeptList.get(i);
                group.setName(list.getName());
                group.setSub(true);
                group.setSelectNum("0");
                group.setCheked(checked);
                group.setSubListss(list.getSubDeptList());
                group.setDeptListss(list.getDeptUserList());
                result.add(group);

            }
        }
        if (null != cUserList) {
            for (int i = 0; i < cUserList.size(); i++) {

                FollowUserGroup group = new FollowUserGroup();
                CompanyUserListReponse.DeptUserList list = cUserList.get(i);
                group.setName(list.getName());
                group.setSub(false);
                group.setId(list.getId());
                group.setCheked(checked);
                result.add(group);
                if (null !=selectfollowuser){

                    for (int j =0;j <selectfollowuser.size();j++){
                         LogUtils.v(TAG+"selectfollowuser:----->"+selectfollowuser.get(j).getName());
                        if (selectfollowuser.get(j).getName().equals(list.getName())){
                            group.setCheked(true);
                        }
                    }
                }
            }
        }
        LogUtils.v(TAG + "result:"+result.toString());
        return result;

    }

}
